package org.finesys.ai.config.openai;

import org.finesys.ai.core.DefaultOpenAiClient;
import org.finesys.ai.core.OpenAiClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.Objects;

@Configuration
@EnableConfigurationProperties({OpenAiProperties.class})
public class OpenAiConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = OpenAiProperties.OPEN_AI_PREFIX, name = "api-key")
    public OpenAiClient openAiClient(OpenAiProperties openAiProperties) {
        DefaultOpenAiClient.Builder builder = DefaultOpenAiClient.builder()
                .baseUrl(openAiProperties.getBaseUrl())
                .model(openAiProperties.getModel())
                .logRequests(openAiProperties.isLogRequests()).logResponses(openAiProperties.isLogResponses());
        if (Objects.nonNull(openAiProperties.getApiKey())) {
            builder.openAiApiKey(openAiProperties.getApiKey());
        }
        if (Objects.nonNull(openAiProperties.getAzureApiKey())) {
            builder.azureApiKey(openAiProperties.getAzureApiKey());
        }
        if (Objects.nonNull(openAiProperties.getApiVersion())) {
            builder.apiVersion(openAiProperties.getApiVersion());
        }

        if (Objects.nonNull(openAiProperties.getDispatcher())) {
            builder.dispatcher(openAiProperties.getDispatcher());
        }
        if (Objects.nonNull(openAiProperties.getUserAgent())) {
            builder.userAgent(openAiProperties.getUserAgent());
        }
        if (Objects.nonNull(openAiProperties.getProxy())) {
            builder.proxy(openAiProperties.getProxy());
        }
        if (Objects.nonNull(openAiProperties.getCallTimeout())) {
            builder.callTimeout(Duration.ofSeconds(openAiProperties.getCallTimeout()));
        }
        if (Objects.nonNull(openAiProperties.getConnectTimeout())) {
            builder.connectTimeout(Duration.ofSeconds(openAiProperties.getConnectTimeout()));
        }
        if (Objects.nonNull(openAiProperties.getReadTimeout())) {
            builder.readTimeout(Duration.ofSeconds(openAiProperties.getReadTimeout()));
        }
        if (Objects.nonNull(openAiProperties.getWriteTimeout())) {
            builder.writeTimeout(Duration.ofSeconds(openAiProperties.getWriteTimeout()));
        }
        if (Objects.nonNull(openAiProperties.getCustomHeaders())) {
            builder.customHeaders(openAiProperties.getCustomHeaders());
        }
        if (Objects.nonNull(openAiProperties.getPersistTo())) {
            builder.persistTo(openAiProperties.getPersistTo());
        }
        builder.logLevel(openAiProperties.getLogLevel());
        builder.logStreamingResponses(openAiProperties.isLogStreamingResponses());

        return builder.build();
    }
}
